package com.xxxx.manager.service.impl;



import com.xxxx.common.utils.JsonUtil;
import com.xxxx.manager.mapper.GoodsCategoryMapper;

import com.xxxx.manager.pojo.GoodsCategory;
import com.xxxx.manager.pojo.GoodsCategoryExample;
import com.xxxx.manager.service.GoodsCategoryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
public class GoodsCategoryServiceimpl implements GoodsCategoryService{


    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 商品分类-查询所有-分类(分层)
     */
    @Override
    public List<GoodsCategory> selectGCategoryList() {

        //查询Redis缓存是否有数据，有数据直接返还，没有则去数据库查询
        String listoutJson=(String) redisTemplate.opsForValue().get("goodsCategoryListkey");

        if (!StringUtils.isEmpty(listoutJson)) {
            return JsonUtil.jsonToList(listoutJson, GoodsCategory.class);
        }

        List<GoodsCategory> list=goodsCategoryMapper.selectByPrimaryId(null);
        System.out.println(list);
        List<GoodsCategory> listout=new ArrayList<>();

/*        for (GoodsCategory i : list){
            if (i.getLevel()==1){
                listout.add(i);//承接数据用于返还到controller层
            }
        }

        return listout;
        */

        int num=0;
        for (GoodsCategory i : list) {//遍历每一个元素

            List<GoodsCategory> list01 = null;//初始化
            if (i.getParentId()==0){//通过判断得出（0）首层没有父节点的一层


                list.set(num,part(list,i,list01));//更新list中的数据利用封装过的方法part

                listout.add(list.get(num));//承接数据用于返还到controller层

                num++;//计数器
            }


        }
        redisTemplate.opsForValue().set("goodsCategoryListkey", JsonUtil.object2JsonStr(listout),2, TimeUnit.DAYS);
        return listout;

    }


    public GoodsCategory part(List<GoodsCategory> list , GoodsCategory i ,List<GoodsCategory> list01 ){

        if (i.getLevel()<3){//判断该元素是否是一个父级节点（拥有下级）


            list01 = new ArrayList<>();//初始化
            for (GoodsCategory j : list) {//当i拥有下级再次遍历将对应下级放入容器

                if (j.getParentId().equals(i.getId())) {//找到子类
                    j=part(list,j,list01);//递归此方法
                    list01.add(j);
                }
            }
            i.setChildren(list01);

            return i;
        }
        return i;
    }

    /**
     * 商品分类-查询-所有分类
     */
    @Override
    public List<GoodsCategory> selectCategoryList01(Short id) {

        String listoutJson=(String) redisTemplate.opsForValue().get("selectCategoryList01");

        if(!StringUtils.isEmpty(listoutJson)){
            return JsonUtil.jsonToList(listoutJson, GoodsCategory.class);

        }
        List<GoodsCategory> list=goodsCategoryMapper.selectByPrimaryId(id);
        redisTemplate.opsForValue().set("selectCategoryList01",JsonUtil.object2JsonStr(list),1,TimeUnit.DAYS);
        return list;
    }

    /**
     * 商品分类-查询所有-顶级分类
     */
    @Override
    public List<GoodsCategory> selectGCategoryTopList() {

        String listoutJson=(String) redisTemplate.opsForValue().get("selectGCategoryTopList");

        if(!StringUtils.isEmpty(listoutJson)){

            return JsonUtil.jsonToList(listoutJson, GoodsCategory.class);

        }
        //创建查询对象
        GoodsCategoryExample example=new GoodsCategoryExample();
        //设置查询条件 ParentId=0；
        example.createCriteria().andParentIdEqualTo((short)0);
       // System.out.println(goodsCategoryMapper.selectByExample(example));
        //System.out.println(1);//测试用打印

        List<GoodsCategory> list=goodsCategoryMapper.selectByExample(example);
        redisTemplate.opsForValue().set("selectGCategoryTopList",JsonUtil.object2JsonStr(list),1,TimeUnit.DAYS);
        return list;
    }


    /**
     * 商品分类-新增分类查询-级联查询
     */
    @Override
    public List<GoodsCategory> selectCategoryByParentId(Short parentid) {
        //创建查询对象
        GoodsCategoryExample example=new GoodsCategoryExample();
        //设置查询条件 ParentId=0；
        example.createCriteria().andParentIdEqualTo(parentid);


        return goodsCategoryMapper.selectByExample(example);
    }


    /**
     * 商品分类-新增分类-保存分类
     */
    @Override
    public int categorySave(GoodsCategory goodsCategory) {

        redisTemplate.delete("goodsCategoryListkey");
        redisTemplate.delete("selectCategoryList01");

        return goodsCategoryMapper.insertSelective(goodsCategory);

    }


}
